import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    meta: {
      title: 'demo'
    },
    children: [
      {
        path: '',
        name: 'demoList',
        meta: {
          title: 'demo列表',
        },
        component: () => import('@/views/demo/DemoList')
      },
      {
        path: '/demoDetail/:id',
        name: 'demoDetail',
        meta: {
          title: 'demo详情',
        },
        component: () => import('@/views/demo/DemoDetail'),
        props: true
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    meta: {
      auth: true // 需要认证
    },
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL, // 基础url http://1.2.3.4/shop
  routes
})

// 全局守卫
router.beforeEach((to, from, next) => {
  if (to.meta.auth && !window.isLogin) {
    if (window.confirm('请登录')) {
      window.isLogin = true
      // 登录成功继续下一步
      next()
    } else {
      // 放弃回首页
      next('/')
    }
  } else {
    // 不需要登录，继续
    next()
  }
})
export default router
